﻿Imports System.Data.Common
Imports CommonLayer
Imports DataAccessLayer

Public Class BLBaseBusiness
    Inherits BLBase

#Region "Declaration"

    Protected m_eRefType As RefType = RefType.None
    Protected oDLBusiness As DLBaseBusiness

    Protected m_strTableDetailName As String
    Protected m_strPrimaryKeyTableDetailName As String
    Protected m_strForeignKeyName As String
    Protected dtBusinessDetail As DataTable

#End Region

#Region "Property"

    Public Overridable ReadOnly Property TableBusinessDetailObject() As DataTable
        Get
            Return dtBusinessDetail
        End Get
    End Property

    Public Overridable Property TableDetailName() As String
        Get
            Return m_strTableDetailName
        End Get
        Set(ByVal value As String)
            m_strTableDetailName = value
            If oDLBusiness IsNot Nothing Then
                oDLBusiness.TableDetailName = value
            End If

            If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(value) Then
                If dsBusiness.Tables(value).PrimaryKey.Length > 0 Then
                    Me.TableDetailPrimaryKey = dsBusiness.Tables(value).PrimaryKey(0).ColumnName
                End If
                dtBusinessDetail = dsBusiness.Tables(value)
            End If
        End Set
    End Property

    Public Property TableDetailPrimaryKey() As String
        Get

            Return m_strPrimaryKeyTableDetailName
        End Get
        Set(ByVal value As String)
            m_strPrimaryKeyTableDetailName = value
            If oDLBusiness IsNot Nothing Then
                oDLBusiness.TableDetailPrimaryKey = value
            End If
        End Set
    End Property

    Public Property TableDetailForeignKey() As String
        Get
            Return m_strForeignKeyName
        End Get
        Set(ByVal value As String)
            m_strForeignKeyName = value
            If oDLBusiness IsNot Nothing Then
                oDLBusiness.TableDetailForeignKey = value
            End If
        End Set
    End Property

#End Region

#Region "Constructor"

#End Region

#Region "Get Method"

#End Region

#Region "CRUD"

    Public Overridable Sub GetDetailDataByMasterID(ByVal strMasterID As Guid)
        Try
            If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableDetailName) Then
                dsBusiness.Tables(Me.TableDetailName).Clear()
                oDLBusiness.GetDetailDataByMasterID(dsBusiness.Tables(Me.TableDetailName), strMasterID)
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

#Region "InitNewRow"

    Public Overridable Function InitNewMasterRow(Optional ByVal strID As Object = Nothing) As DataRow
        Return Nothing
    End Function


#End Region

#End Region

#Region "Private Method"

#End Region

#Region "Other Method"

    Public Overrides Function SetNewRefNo(ByVal drMaster As DataRow, ByVal ts As DbTransaction) As Boolean
        If drMaster.Table.Columns.Contains("RefType") AndAlso drMaster.Table.Columns.Contains("RefNo") Then
            Return oDLBusiness.UpdateAutoID(drMaster("RefType"), drMaster("RefNo").ToString, ts)
        Else
            Return True
        End If
    End Function

    Public Overrides Sub RejectChange()
        If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableDetailName) Then
            dsBusiness.Tables(Me.TableDetailName).RejectChanges()
        End If

        MyBase.RejectChange()
    End Sub

    Public Overrides Sub AcceptChange()
        MyBase.AcceptChange()

        If dsBusiness IsNot Nothing AndAlso dsBusiness.Tables.Contains(Me.TableDetailName) Then
            dsBusiness.Tables(Me.TableDetailName).AcceptChanges()
        End If
    End Sub

    Public Overrides Sub InitPublicObject()
    End Sub

#End Region
End Class
